package com.edu.scau.dao.implement;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;
import javax.annotation.*;
import com.edu.scau.dao.OrderFormDao;
import com.edu.scau.entity.Commodity;
import com.edu.scau.entity.OrderForm;
import com.edu.scau.entity.User;

@Component
public class OrderFormDaoImp implements OrderFormDao{
	
	@Resource(name="sessionFactory")
	private SessionFactory  factory; 
	 
	 
	 
	 
	public SessionFactory getFactory() {
		return factory;
	}

	public void setFactory(SessionFactory factory) {
		this.factory = factory;
	}

	public boolean save(OrderForm orderform) {
		// TODO Auto-generated method stub
		try {
			Session session=factory.getCurrentSession();
			session.save(orderform);
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		return true;
	}

	public boolean delete(OrderForm orderform) {
		try {
			// TODO Auto-generated method stub
			Session session=factory.getCurrentSession();
			session.delete(orderform);
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		return true;
	}

	public List<OrderForm> queryByUserId(int userId) {
		// TODO Auto-generated method stub
		Session session=factory.getCurrentSession();
		List<User> form=new ArrayList<User>();
		Criteria criteria=session.createCriteria(User.class);
		form=(ArrayList<User>) criteria.add(Restrictions.like("id", userId)).list();
		if(form.isEmpty())
		return null;
		//System.out.println(form.get(0).getName());
		//return null;
		return  queryByUser(form.get(0));
	}

	@SuppressWarnings("unchecked")
	public List<OrderForm> queryByUser(User user) {
		// TODO Auto-generated method stub
		Session session=factory.getCurrentSession();
		List<OrderForm> form=new ArrayList<OrderForm>();
		Criteria criteria=session.createCriteria(OrderForm.class);
		form=(ArrayList<OrderForm>) criteria.add(Restrictions.like("user", user)).list();
		return form;
	}

	

	public boolean update(OrderForm orderform) {
		// TODO Auto-generated method stub
		try {
			// TODO Auto-generated method stub
			Session session=factory.getCurrentSession();
			session.update(orderform);
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		return true;
		
	}

	public OrderForm queryOrderFormById(Integer orderFormId) {
		// TODO Auto-generated method stub
		Session session=factory.getCurrentSession();
		List<OrderForm> form=new ArrayList<OrderForm>();
		Criteria criteria=session.createCriteria(OrderForm.class);
		form=(ArrayList<OrderForm>) criteria.add(Restrictions.like("id",  orderFormId)).list();
		
		if(form.isEmpty())
		return null;
		//System.out.println(form.get(0).getName());
		//return null;
		return form.get(0);
		
	}

	public List<OrderForm> queryCommoditiesByFormId(Integer formId) {
		// TODO Auto-generated method stub
		Session session=factory.getCurrentSession();
		List<OrderForm> form=new ArrayList<OrderForm>();
		Criteria criteria=session.createCriteria(OrderForm.class);
		form=(ArrayList<OrderForm>) criteria.add(Restrictions.like("id", formId)).list();
		return form;
		
	}



	

}
